package com.example.project.projectspringcloud.controller.admin.user;

import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.user.Collect;
import com.example.project.projectspringcloud.service.user.UserCollectService;
import com.example.project.projectspringcloud.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "后台用户收藏管理")
@Controller
@RequestMapping(value = "/admin/user/collect")
public class AdminUserCollectController {

    @Autowired
    private UserCollectService userCollectService;

    @ApiOperation(value = "后台用户收藏管理列表", notes = "返回列表")
    @GetMapping(value = {"", "/list"})
    public String list(Collect collect, Page page, Model model) {
        List<Collect> collects = userCollectService.list(page, collect);
        model.addAttribute("page", new Page<Collect>(collects));
        model.addAttribute("collect", collect);
        return "admin/collect/collect-list";
    }

    @ApiOperation(value = "添加评论", notes = "管理员添加评论")
    @PostMapping(value = "/api/save")
    @ResponseBody
    public RespBody save(Collect collect) throws OperationException {
        boolean result = userCollectService.save(collect);
        return RespBody.ok(result);
    }

    @ApiOperation(value = "删除", notes = "通过id数组删除")
    @DeleteMapping(value = "/api/delete/{ids}")
    @ResponseBody
    public RespBody delete(@ApiParam(value = "字符串ID数组 1,2,3...") @PathVariable("ids") String ids) {
        boolean result = userCollectService.delete(StringUtil.split(ids, ","));
        return RespBody.ok(result);
    }

}
